package com.hl.icd.service.impl;

import com.github.pagehelper.PageHelper;
import com.hl.icd.bean.User;
import com.hl.icd.dao.UserDao;
import com.hl.icd.exception.ICDEnum;
import com.hl.icd.exception.ICDException;
import com.hl.icd.service.UserService;
import com.hl.icd.utils.DateTimeUtil;
import com.hl.icd.utils.MD5Util;
import com.hl.icd.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(User user) throws ICDException{
        String password = user.getPassword();
        password = MD5Util.getMD5(password);
        user.setPassword(password);
        List<User> userList = userDao.select(user);
        if(userList.size() == 0){
            throw new ICDException(ICDEnum.USER_LOGIN_ACCOUNT);
        }
        return userList.get(0);
    }

    @Override
    public void signUp(User user) throws ICDException{
        user.setId(UUIDUtil.getUUID());
        user.setCreateBy(user.getUsername());
        user.setCreateTime(DateTimeUtil.getSysTime());
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        user.setLockState("1");
        int res = userDao.insert(user);
        if(res != 1){
            throw new ICDException(ICDEnum.USER_SIGN_UP_FAILED);
        }
    }

    @Override
    public void signUp(User user, String role) {
        user.setId(UUIDUtil.getUUID());
        user.setCreateBy(user.getUsername());
        user.setCreateTime(DateTimeUtil.getSysTime());
        user.setEditBy(user.getUsername());
        user.setEditTime(DateTimeUtil.getSysTime());
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        user.setLockState("1");
        user.setRole(role);
        int res = userDao.insert(user);
        if(res != 1){
            throw new ICDException(ICDEnum.USER_SIGN_UP_FAILED);
        }
    }

    @Override
    public List<User> userQuery(int pageNum, int pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);
        return userDao.getUserListByCondition(user);
    }

    @Override
    public String insertOne(User user) {
        String message="error";
        user.setId(UUIDUtil.getUUID());
        user.setCreateBy(user.getUsername());
        user.setCreateTime(DateTimeUtil.getSysTime());
        user.setEditTime(DateTimeUtil.getSysTime());
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        int res = userDao.insert(user);
        if(res == 1){
            message = "success";
        }
        return message;
    }

    @Override
    public String updateOne(User user) {
        String message="error";
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        user.setEditTime(DateTimeUtil.getSysTime());
        int res = userDao.updateByPrimaryKey(user);
        if(res == 1){
            message="success";
        }
        return message;
    }

    @Override
    public String deleteOneById(String id) {
        String message="error";
        int res = userDao.deleteByPrimaryKey(id);
        if(res == 1){
            message = "success";
        }
        return message;
    }

    @Override
    public String deleteBatch(List<String> ids) {
        String message="success";
        int res=0;
        for (String id : ids) {
            res = userDao.deleteByPrimaryKey(id);
            if(res!=1){
                message = "error";
                return message;
            }
        }
        return message;
    }

    @Override
    public String updateState(String id, String state) {
        String message="error";
        int res = userDao.updateState(id,state);
        if(res == 1){
            message = "success";
        }
        return message;
    }
}
